package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.BbsBoard;
import la.bbs558.entity.enums.OssFolder;
import la.bbs558.entity.params.BoardSearchPage;
import la.bbs558.entity.params.BoardUpdateParams;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.BbsBoardService;
import la.bbs558.util.OssUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: Shanks
 * @date: 2023/12/16 12:27
 * @desc:
 */
@RestController
@RequestMapping("/admin/board")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
@Check
public class BbsBoardManageController {

    private final BbsBoardService boardService;

    private final OssUtil ossUtil;

    @GetMapping("/list")
    public Result<?> list(BoardSearchPage searchPage) {
        boardService.listByPageForAdmin(searchPage);
        return Result.success(searchPage);
    }

    @PostMapping("upload")
    public Result<?> uploadAdImage(MultipartFile file) {
        String key = ossUtil.uploadFile(file, OssFolder.Board);
        return Result.success(key);
    }

    @GetMapping("/get")
    public Result<?> getBoardById(Long id) {
        return Result.success(boardService.getBoardById(id, true));
    }

    @PostMapping("/add")
    public Result<?> add(@RequestBody @Valid BoardUpdateParams updateParams) {
        boardService.addBoardForAdmin(updateParams);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody @Valid BoardUpdateParams updateParams) {
        boardService.updateBoardForAdmin(updateParams);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result<?> delete(Long id) {
        boardService.deleteBoardByIdForAdmin(id);
        return Result.success();
    }

    @PostMapping("/all")
    public Result<?> all() {
        return Result.success(boardService.lambdaQuery()
                .select(BbsBoard::getId, BbsBoard::getBoardName)
                .list());
    }
}
